home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / INFO / FAQP9317.ZIP / progfaq.3 < prev    next >
Text File  |  1993-09-25  |  58KB  |  1,227 lines

  1. Newsgroups: comp.os.msdos.programmer,comp.answers,news.answers
  2. Subject: comp.os.msdos.programmer FAQ part 3 of 4
  3. Expires: +27 days
  4. Followup-To: comp.os.msdos.programmer
  5. Distribution: world
  6. Message-ID: <msdos-faq.9317.3@NCoast.ORG>
  7. References: <msdos-faq.9317.2@NCoast.ORG>
  8. Supersedes: <msdos-faq.9316.3@NCoast.ORG>
  9. Approved: news-answers-request@MIT.Edu
  10. Keywords:
  11.  
  12. Archive-name: msdos-programmer-faq/part3
  13. Last-modified: 24 Sep 1993
  14.  
  15.  
  16. (continued from part 2)         (no warranty on the code or information)
  17.  
  18. If the posting date is more than six weeks in the past, see instructions
  19. in part 4 of this list for how to get an updated copy.
  20.  
  21. Copyright (C) 1993  Stan Brown, Oak Road Systems.  All rights reserved.
  22.  
  23.  
  24. section 4.  Disks and files
  25. ===========================
  26.  
  27. Subject:  401. What drive was the PC booted from?
  28.  
  29.     Under DOS 4.0 or later, load 3305 hex into AX; do an INT 21.  DL is
  30.     returned with an integer indicating the boot drive (1=A:, etc.).
  31.  
  32. Subject:  402. How can I boot from drive b:?
  33.  
  34.     (rev: 9 Aug 1993)  Downloadable shareware:
  35.         pd1:<msdos.dskutl>boot_b.zip from Simtel
  36.         /pc/bootutil/boot_b.zip from Garbo.
  37.     The included documentation says it works by writing a new boot
  38.     sector on a disk in your a: drive that redirects the boot to your
  39.     b: drive.  (A similar utility is bboot.zip in the same directory at
  40.     Garbo only.)
  41.  
  42.     If that doesn't work, you can always interchange your a: and b:
  43.     drives by switching ribbon cables and changing the setup in your
  44.     BIOS.  From an article posted 27 Jan 1993 on another newsgroup:
  45.  
  46.     Take the "ribbon" connector, as you call it, and switch them.  To
  47.     double check, start at the end of the cable that connects to the
  48.     motherboard or floppy controller.  Follow the cable until you get to
  49.     the first connector.  Connect this to the drive you want to be b:.
  50.     After this, there should be a few lines on the cable that get
  51.     flipped left to right.  (On most cables, they just cut the lines and
  52.     physically reverse them.  It should be quite obvious from looking at
  53.     the cable.)  Anyway, the connector after the pins get flipped
  54.     right to left is the connector for your a: drive.
  55.  
  56. Subject:  403. Which real and virtual disk drives are valid?
  57.  
  58.     (rev: 15 Aug 1993)  Use INT 21 function 29 (parse filename).  Point
  59.     DS:SI at a null-terminated ASCII string that contains the drive
  60.     letter and a colon, point ES:DI at a 37-byte dummy FCB buffer, set
  61.     AX to 2900h, and do an INT 21.  On return, AL is FF if the drive is
  62.     invalid, something else if the drive is valid.  RAM disks and
  63.     SUBSTed drives are considered valid.
  64.  
  65.     You can detect whether the drive is ASSIGNed by using INT 2F
  66.     AX=0601.  To check whether the drive is SUBSTed, use INT 21 AX=4409;
  67.     or use INT 21 function 52 to test for both JOIN and SUBST.  See Ralf
  68.     Brown's interrupt list.
  69.  
  70.     Unfortunately, the b: drive is considered valid even on a single-
  71.     diskette system.  You can check that special case by interrogating
  72.     the BIOS equipment byte at 0040:0010.  Bits 7-6 contain the one less
  73.     than the number of diskette drives, so if those bits are zero you
  74.     know that b: is an invalid drive even though function 29 says it's
  75.     valid.
  76.  
  77.     Following is some code originally posted by Doug Dougherty to test
  78.     valid drives (without regard to SUBST and JOIN), with SB's fix for
  79.     the b: special case, tested in Borland C++ 2.0 (in the small model):
  80.  
  81.         #include <dos.h>
  82.         void drvlist(void)  {
  83.             char *s = "A:", fcb_buff[37];
  84.             int valid;
  85.             for (   ;  *s<='Z';  (*s)++) {
  86.                 _SI = (unsigned) s;
  87.                 _DI = (unsigned) fcb_buff;
  88.                 _ES = _DS;
  89.                 _AX = 0x2900;
  90.                 geninterrupt(0x21);
  91.                 valid = _AL != 0xFF;
  92.                 if (*s == 'B'  &&  valid) {
  93.                     char far *equipbyte = (char far *)0x00400010UL;
  94.                     valid = (*equipbyte & (3 << 6)) != 0;
  95.                 }
  96.                 printf("Drive '%s' is %sa valid drive.\n",
  97.                         s, valid ? "" : "not ");
  98.             }
  99.         }
  100.  
  101.     SB translated this to MSC 7.0 and tested it in small model:
  102.  
  103.         #include <dos.h>
  104.         #include <stdio.h>
  105.         void drvlist(void)  {
  106.             char *s = "A:", fcb_buff[37], *buff=fcb_buff;
  107.             int valid;
  108.             for (   ;  *s<='Z';  (*s)++) {
  109.                 __asm mov si,s         __asm mov di,buff
  110.                 __asm mov ax,ds        __asm mov es,ax
  111.                 __asm mov ax,0x2900    __asm int 21h
  112.                 __asm xor ah,ah        __asm mov valid,ax
  113.                 valid = (valid != 0xFF);
  114.                 if (*s == 'B'  &&  valid) {
  115.                     char far *equipbyte = (char far *)0x00400010UL;
  116.                     valid = (*equipbyte & (3 << 6)) != 0;
  117.                 }
  118.                 printf("Drive '%s' is %sa valid drive.\n",
  119.                         s, valid ? "" : "not ");
  120.             }
  121.         }
  122.  
  123. Subject:  404. How can I make my single floppy drive both a: and b:?
  124.  
  125.     Under any DOS since DOS 2.0, you can put the command
  126.  
  127.         assign b=a
  128.  
  129.     into your AUTOEXEC.BAT file.  Then, when you type "DIR B:" you'll no
  130.     longer get the annoying prompt to insert diskette B (and the even
  131.     more annoying prompt to insert A the next time you type "DIR A:").
  132.  
  133.     You may be wondering why anybody would want to do this.  Suppose you
  134.     use two different machines, maybe one at home and one at work.  One
  135.     of them has only a 3.5" diskette drive; the other machine has two
  136.     drives, and b: is the 3.5" one.  You're bound to type "dir b:" on
  137.     the first one, and get the nuisance message
  138.  
  139.         Insert diskette for drive B: and press any key when ready.
  140.  
  141.     But if you assign drive b: to point to a:, you avoid this problem.
  142.  
  143.     Caution:  there are a few commands, such as DISKCOPY, that will not
  144.     work right on ASSIGNed or SUBSTed drives.  See the DOS manual for
  145.     the full list.  Before typing one of those commands, be sure to turn
  146.     off the mapping by typing "assign" without arguments.
  147.  
  148.     The DOS 5.0 manual says that ASSIGN is obsolete, and recommends the
  149.     equivalent form of SUBST: "subst b: a:\".  Unfortunately, if this
  150.     command is executed when a: doesn't hold a diskette, the command
  151.     fails.  ASSIGN doesn't have this problem, so under DOS 5.0 you
  152.     should disregard that particular bit of advice in the manual.
  153.  
  154. Subject:  405. How can I disable access to a drive?
  155.  
  156.     (new: 15 Aug 1993)  Reader Eric DeVolder writes that he has made
  157.     available a program to do this.  It's downloadable from Simtel as
  158.         pd1:<msdos.dskutl>rmdriv20.zip from Simtel
  159.         /pc/sysutil/rmdriv20.zip at Garbo.
  160.     (existence verified; files not tested by SB)
  161.  
  162. Subject:  406. How can a batch file test existence of a directory?
  163.  
  164.     (new: 28 Aug 1993)  The standard way, which in fact is documented in
  165.     the DOS manual, is
  166.  
  167.         if exist d:\path\nul goto found
  168.  
  169.     Unfortunately, this is not entirely reliable.  SB found it failed in
  170.     Pathworks (a/k/a PCSA, DEC's network that connects PCs and VAXes),
  171.     or on a MARS box that uses an OEM version of MS-DOS 5.0.  Readers
  172.     have reported that it failed on Novell networks or on DR-DOS.
  173.  
  174.     There appears to be no foolproof way to use pure batch commands to
  175.     test for existence of a directory.  The real solution is to write a
  176.     program, which returns a value that your batch program can then test
  177.     with an if errorlevel.  Reader Duncan Murdoch kindly posted the
  178.     following Turbo Pascal version:
  179.  
  180.         program existdir;
  181.         { Confirms the existence of a directory given on the command line.
  182.           Returns errorlevel 2 on error, 1 if not found, 0 if found. }
  183.  
  184.         uses
  185.           dos;
  186.  
  187.         var
  188.           s : searchrec;
  189.  
  190.         begin
  191.           if paramcount <> 1 then
  192.           begin
  193.             writeln('Syntax:  EXISTDIR directory');
  194.             halt(2);
  195.           end
  196.           else
  197.           begin
  198.             findfirst(paramstr(1),Directory,S);
  199.             while (Doserror = 0) and ((Directory and S.Attr) = 0) do
  200.               findnext(S);
  201.             if Doserror <> 0 then
  202.             begin
  203.               Writeln('Directory not found.');
  204.               halt(1);
  205.             end
  206.             else
  207.             begin
  208.               Writeln('Directory found.');
  209.               halt(0);
  210.             end;
  211.           end;
  212.         end.
  213.  
  214.     Timo Salmi also has a Turbo Pascal version in his Turbo Pascal FAQ,
  215.     which is downloadable as
  216.         /pc/ts/tsfaqp15.zip at Garbo
  217.         pd1:<msdos.info>tsfaqp15.zip at Simtel.
  218.  
  219. Subject:  407. Why won't my C program open a file with a path?
  220.  
  221.     You've probably got something like the following code:
  222.  
  223.         char *filename = "c:\foo\bar\mumble.dat";
  224.         . . .  fopen(filename, "r");
  225.  
  226.     The problem is that \f is a form feed, \b is a backspace, and \m is
  227.     m.  Whenever you want a backslash in a string constant in C, you
  228.     must use two backslashes:
  229.  
  230.         char *filename = "c:\\foo\\bar\\mumble.dat";
  231.  
  232.     This is a feature of every C compiler, because Dennis Ritchie
  233.     designed C this way.  It's a problem only on MS-DOS systems, because
  234.     only DOS (and Atari ST/TT running TOS) uses the backslash in
  235.     directory paths.  But even in DOS this backslash convention applies
  236.     _only_ to string constants in your source code.  For file and
  237.     keyboard input at run time, \ is just a normal character, so users
  238.     of your program would type in file specs at run time the same way as
  239.     in DOS commands, with single backslashes.
  240.  
  241.     Another possibility is to code all paths in source programs with /
  242.     rather than \ characters:
  243.  
  244.         char *filename = "c:/foo/bar/mumble.dat";
  245.  
  246.     Ralf Brown writes that "All versions of the DOS kernel accept either
  247.     forward or backslashes as directory separators.  I tend to use this
  248.     form more frequently than backslashes since it is easier to type and
  249.     read."  This applies to DOS function calls (and therefore to calls
  250.     to the file library of every programming language), but not to DOS
  251.     commands.
  252.  
  253. Subject:  408. How can I redirect printer output to a file?
  254.  
  255.     (rev: 16 Aug 1993)  Recommended: PRN2FILE from {PC Magazine},
  256.     downloadable as:
  257.         pd1:<msdos.printer>prn2file.zip at Simtel
  258.         /pc/printer/prn2file.zip at Garbo.
  259.     {PC Magazine} has given copies away as part of its utilities disks,
  260.     so you may already have a copy.
  261.  
  262.     The directories mentioned above have lots of other utilities to
  263.     redirect printer output.
  264.  
  265. Subject:  409. How can I redirect the output of a batch file?
  266.  
  267.     (new: 12 June 1993) Assuming the batch file is called batch.bat, to
  268.     send its output (stdout) to another file, just invoke COMMAND.COM as
  269.     a secondary command processor:
  270.  
  271.         command /c batch parameters_if_any >outfile
  272.  
  273.     Timo Salmi's notes on this and other batch tricks are downloadable:
  274.         pd1:<msdos.batutl>tsbat43.zip at Simtel
  275.         /pc/ts/tsbat43.zip at Garbo.
  276.  
  277. Subject:  410. How can I redirect stderr?
  278.  
  279.     (new: 15 Aug 1993)  Use freopen(..., stderr) and then execute the
  280.     desired command via system( ).  There are downloadable versions of
  281.     programs to do this.  Recommended by SB:
  282.         pd1:<msdos.sysutl>rdstderr.zip from Simtel.
  283.     Source code (in Turbo Pascal 4.0) and executable are included.
  284.  
  285.     A C example is downloadable as
  286.         pd1:<msdos.c>redirect.c from Simtel.
  287.     SB compiled it with MSC 7.0, and it works fine with one exception:
  288.     Contrary to the included comments, redirected output starts writing
  289.     at the beginning of the output file rather than appending.  That is
  290.     easily solved by adding "fseek(stderr, 0L, SEEK_END);" after the
  291.     freopen( ) call for stderr.
  292.  
  293. Subject:  411. How can my program open more files than DOS's limit of 20?
  294.  
  295.     (rev: 12 Sep 1993) This is a summary of an article Ralf Brown posted
  296.     on 8 August 1992, with some additions from a Microsoft tech note.)
  297.  
  298.     DOS imposes some limits.  Once you overcome those, which is pretty
  299.     easy, you may have to take additional measures to overcome the
  300.     limitations built into your compiler's run-time library.
  301.  
  302.     1) Limitations imposed by DOS
  303.  
  304.     There are separate limits on files and file handles.  For example,
  305.     DOS opens three files but five file handles:  CON (stdin, stdout,
  306.     and stderr), AUX (stdaux), and PRN (stdprn).
  307.  
  308.     The limit in FILES= in CONFIG.SYS is a system-wide limit on files
  309.     opened by all programs (including the three that DOS opens and any
  310.     opened by TSRs); each process has a limit of 20 handles (including
  311.     the five that DOS opens).  Example:  CONFIG.SYS has FILES=40.  Then
  312.     program #1 will be able to open 15 file handles.  Assuming that the
  313.     program actually does open 15 handles pointing to 15 different
  314.     files, other programs could still open a total of 22 files (40-3-15
  315.     = 22), though no one program could open more than 15 file handles.
  316.  
  317.     If you're running DOS 3.3 or later, you can increase the per-process
  318.     limit of 20 file handles by a call to INT 21 function 67, Set Handle
  319.     Count.  Your program is still limited by the system-wide limit on
  320.     open files, so you may also need to increase the FILES= value in
  321.     your CONFIG.SYS file (and reboot).  The run-time library that you're
  322.     using may have a fixed-size table of file handles, so you may also
  323.     need to get source code for the module that contains the table,
  324.     increase the table size, and recompile it.
  325.  
  326.     2) Limitations in Microsoft C run-time library
  327.  
  328.     In Microsoft C the run-time library limits you to 20 file handles.
  329.     To change this, you must be aware of two limits:
  330.  
  331.     - file handles used with _open( ), _read( ), etc.: Edit _NFILE_ in
  332.       CRT0DAT.ASM.
  333.  
  334.     - stream files used with fopen( ), fread( ), etc.: Edit _NFILE_ in
  335.       _FILE.C for DOS or FILE.ASM for Windows/QuickWin.  This must not
  336.       exceed the value of _NFILE_ in CRT0DAT.ASM.
  337.  
  338.     (QuickWin uses the constant _WFILE_ in CRT0DAT.ASM and WFILE.ASM for
  339.     the maximum number of child text windows.)
  340.  
  341.     After changing the limits, recompile using CSTARTUP.BAT.  Microsoft
  342.     recommends that you first read README.TXT in the same directory.
  343.  
  344.     3) Limitations in Borland C++ run-time library
  345.  
  346.     (Reader Chin Huang provided this information on 12 Sep 1993.)
  347.  
  348.     To increase the open file limit for a program you compile with
  349.     Borland C++ 3.1, edit the file _NFILE.H in the include directory and
  350.     change the _NFILE_ value.  Compile and link the modules FILES.C and
  351.     FILES2.C from the lib directory into your program.
  352.  
  353. Subject:  412. How can I read, create, change, or delete the volume
  354.                label?
  355.  
  356.     In DOS 5.0 (and possibly in 4.0 as well), there are actually two
  357.     volume labels: one, the traditional one, is an entry in the root
  358.     directory of the disk; and the other is in the boot record along
  359.     with the serial number (see next Q).  The DIR and VOL commands
  360.     report the traditional label; the LABEL command reports the
  361.     traditional one but changes both of them.
  362.  
  363.     In DOS 4.0 and later, use INT 21 function 69 to access the boot
  364.     record's serial number and volume label together; see the next Q.
  365.  
  366.     Assume that by "volume label" you mean the traditional one, the one
  367.     that DIR and VOL display.  Though it's a directory entry in the root
  368.     directory, you can't change it using the newer DOS file-access
  369.     functions (3C, 41, 43); instead, use the old FCB-oriented directory
  370.     functions.  Specifically, you need to allocate a 64-byte buffer and
  371.     a 41- byte extended FCB (file control block).  Call INT 21 AH=1A to
  372.     find out whether there is a volume label.  If there is, AL returns 0
  373.     and you can change the label using DOS function 17 or delete it
  374.     using DOS function 13.  If there's no volume label, function 1A will
  375.     return FF and you can create a label via function 16.  Important
  376.     points to notice are that ? wildcards are allowed but * are not; the
  377.     volume label must be space filled not null terminated.
  378.  
  379.     The following MSC 7.0 code worked for SB in DOS 5.0; the functions
  380.     it uses have been around since DOS 2.0.  The function parameter is 0
  381.     for the current disk, 1 for a:, 2 for b:, etc.  It doesn't matter
  382.     what your current directory is; these functions always search the
  383.     root directory for volume labels.  (I didn't try to change the
  384.     volume label of any networked drives.)
  385.  
  386.     // Requires DOS.H, STDIO.H, STRING.H
  387.     void vollabel(unsigned char drivenum) {
  388.         static unsigned char extfcb[41], dta[64], status, *newlabel;
  389.         int chars_got = 0;
  390.         #define DOS(buff,func) __asm { __asm mov dx,offset buff \
  391.             __asm mov ax,seg buff  __asm push ds  __asm mov ds,ax \
  392.             __asm mov ah,func  __asm int 21h  __asm pop ds \
  393.             __asm mov status,al }
  394.         #define getlabel(buff,prompt) newlabel = buff;  \
  395.             memset(newlabel,' ',11);  printf(prompt);   \
  396.             scanf("%11[^\n]%n", newlabel, &chars_got);  \
  397.             if (chars_got < 11) newlabel[chars_got] = ' ';
  398.  
  399.         // Set up the 64-byte transfer area used by function 1A.
  400.         DOS(dta, 1Ah)
  401.         // Set up an extended FCB and search for the volume label.
  402.         memset(extfcb, 0, sizeof extfcb);
  403.         extfcb[0] = 0xFF;             // denotes extended FCB
  404.         extfcb[6] = 8;                // volume-label attribute bit
  405.         extfcb[7] = drivenum;         // 1=A, 2=B, etc.; 0=current drive
  406.         memset(&extfcb[8], '?', 11);  // wildcard *.*
  407.         DOS(extfcb,11h)
  408.         if (status == 0) {            // DTA contains volume label's FCB
  409.             printf("volume label is %11.11s\n", &dta[8]);
  410.             getlabel(&dta[0x18], "new label (\"delete\" to delete): ");
  411.             if (chars_got == 0)
  412.                 printf("label not changed\n");
  413.             else if (strncmp(newlabel,"delete     ",11) == 0) {
  414.                 DOS(dta,13h)
  415.                 printf(status ? "label failed\n" : "label deleted\n");
  416.             }
  417.             else {                    // user wants to change label
  418.                 DOS(dta,17h)
  419.                 printf(status ? "label failed\n" : "label changed\n");
  420.             }
  421.         }
  422.         else {                        // no volume label was found
  423.             printf("disk has no volume label.\n");
  424.             getlabel(&extfcb[8], "new label (<Enter> for none): ");
  425.             if (chars_got > 0) {
  426.                 DOS(extfcb,16h)
  427.                 printf(status ? "label failed\n" : "label created\n");
  428.             }
  429.         }
  430.     }   // end function vollabel
  431.  
  432. Subject:  413. How can I get the disk serial number?
  433.  
  434.     Use INT 21.  AX=6900 gets the serial number; AX=6901 sets it.  See
  435.     Ralf Brown's interrupt list, or page 496 of {PC Magazine} July 1992,
  436.     for details.
  437.  
  438.     This function also gets and sets the volume label, but it's the
  439.     volume label in the boot record, not the volume label that a DIR
  440.     command displays.  See the preceding Q.
  441.  
  442. Subject:  414. What's the format of .OBJ, .EXE., .COM files?
  443.  
  444.     Please see section 2, "Compile and link".
  445.  
  446. Subject:  415. How can I flush the software disk cache?
  447.  
  448.     Please see "How can a program reboot my PC?" in section 7, "Other
  449.     software questions and problems".
  450.  
  451. section 5. Serial ports (COM ports)
  452. ===================================
  453.  
  454. Subject:  501. How do I set my machine up to use COM3 and COM4?
  455.  
  456.     Unless your machine is fairly old, it's probably already set up.
  457.     After installing the board that contains the extra COM port(s),
  458.     check the I/O addresses in word 0040:0004 or 0040:0006.  (In DEBUG,
  459.     type "D 40:4 L4" and remember that every word is displayed low
  460.     byte first, so if you see "03 56" the word is 5603.)  If those
  461.     addresses are nonzero, your PC is ready to use the ports and you
  462.     don't need the rest of this answer.
  463.  
  464.     If the I/O address words in the 0040 segment are zero after you've
  465.     installed the I/O board, you need some code to store these values
  466.     into the BIOS data segment:
  467.  
  468.         0040:0004  word  I/O address of COM3
  469.         0040:0006  word  I/O address of COM4
  470.         0040:0011  byte (bits 3-1): number of serial ports installed
  471.  
  472.     The documentation with your I/O board should tell you the port
  473.     addresses.  When you know the proper port addresses, you can add
  474.     code to your program to store them and the number of serial ports
  475.     into the BIOS data area before you open communications.  Or you can
  476.     use DEBUG to create a little program to include in your AUTOEXEC.BAT
  477.     file, using this script:
  478.  
  479.             n SET_ADDR.COM      <--- or a different name ending in .COM
  480.             a 100
  481.             mov  AX,0040
  482.             mov  DS,AX
  483.             mov  wo [0004],aaaa <--- replace aaaa with COM3 address or 0
  484.             mov  wo [0006],ffff <--- replace ffff with COM4 address or 0
  485.             and  by [0011],f1
  486.             or   by [0011],8    <--- use number of serial ports times 2
  487.             mov  AH,0
  488.             int  21
  489.                                 <--- this line must be blank
  490.             rCX
  491.             1f
  492.             rBX
  493.             0
  494.             w
  495.             q
  496.  
  497. Subject:  502. How do I find the I/O address of a COM port?
  498.  
  499.     (rev: 15 Aug 1993)  Look in the four words beginning at 0040:0000
  500.     for COM1 through COM4.  (The DEBUG command "D 40:0 L8" will do this.
  501.     Remember that words are stored and displayed low byte first, so a
  502.     word value of 03F8 will be displayed as F8 03.)  If the value is
  503.     zero, that COM port is not installed (or you've got an old BIOS; see
  504.     the preceding Q).  If the value is nonzero, it is the I/O address of
  505.     the transmit/receive register for the COM port.  Each COM port
  506.     occupies eight consecutive I/O addresses (though only the first
  507.     seven are used by many chips).
  508.  
  509.     Here's some C code to find the I/O address:
  510.  
  511.         unsigned ptSel(unsigned comport) {
  512.             unsigned io_addr;
  513.             if (comport >= 1  &&  comport <= 4) {
  514.                 unsigned far *com_addr = (unsigned far *)0x00400000UL;
  515.                 io_addr = com_addr[comport-1];
  516.             }
  517.             else
  518.                 io_addr = 0;
  519.             return io_addr;
  520.         }
  521.  
  522.     You might also want to explore Port Finder, downloadable as
  523.  
  524.         pd1:<msdos.sysutl>pf271.zip at Simtel
  525.         /pub/msdos/utilities/sysutl/pf271.zip at nic.funet.fi
  526.  
  527.     I (SB) haven't tried it myself, but a posted article reviewed it
  528.     very favorably and said it also lets you swap ports around.
  529.  
  530. Subject:  503. But aren't the COM ports always at I/O addresses 3F8,
  531.                2F8, 3E8, and 2E8?
  532.  
  533.     The first two are usually right (though not always); the last two
  534.     are different on many machines.
  535.  
  536. Subject:  504. How do I configure a COM port and use it to transmit data?
  537.  
  538.     (rev: 17 Sep 1993)  Do you want actual code, or do you want books
  539.     that explain what's going on?
  540.  
  541.     1) Source code
  542.  
  543.     First, check your compiler's run-time library.  Many compilers offer
  544.     functions similar to Microsoft C's _bios_serialcom() or Borland's
  545.     bioscom(), which may meet your needs.
  546.  
  547.     Second, check for downloadable resources at Simtel and Garbo.  At
  548.     Simtel, pd1:<msdos.c>pcl4c34.zip (March 1993) is described as
  549.     "Asynchronous communications library for C"; Garbo has a whole
  550.     /pc/comm directory.  Also, an extended example is in Borland's
  551.     TechFax TI445, downloadable as part of
  552.         pd1:<msdos.turbo-c>bchelp10.zip at Simtel
  553.         /pc/turbopas/bchelp10.zip at Garbo.
  554.     Though written by Borland, much of it is applicable to other forms
  555.     of C, and it should give you ideas for other programming languages.
  556.  
  557.     2) Reference books
  558.  
  559.     Highly recommended: Joe Campbell's {C Programmer's Guide to Serial
  560.     Communications}, ISBN 0-672-22584-0.  He gives complete details on
  561.     how serial ports work, along with complete programs for doing polled
  562.     or interrupt-driver I/O.  The book is quite thick, and none of it
  563.     looks like filler.
  564.  
  565.     If Campbell's book is overkill for you, you'll find a good short
  566.     description of serial I/O in {DOS 5: A Developer's Guide}, ISBN
  567.     1-55851-177-6, by Al Williams.
  568.  
  569.     Finally, a reader has recommended {Serial Communications Programming
  570.     in C/C++} by Mark Goodwin (ISBN 1558281983), with source code in the
  571.     book and on disk.  Topics include the basics, various methods of
  572.     serial communications on the PC (with consideration of high-speed
  573.     modems), ANSI screen interface, file transfer protocols (Xmodem and
  574.     Ymodem), etc.  There is code in C, and that code is extended into a
  575.     C++ class for those who use C++.  There are also subroutines in
  576.     Assembly.
  577.  
  578.     3) Downloadable information files
  579.  
  580.     A "Serial Port FAQ" is occasionally posted to this newsgroup.  You
  581.     can get a copy by ftp from pfsparc02.phil15.uni-sb.de.  Look for
  582.     file names *Serial* in directory /pub/E-Technik/afd .  (The archive
  583.     administrator warns that the ftp address may change, sometime in
  584.     the future, to etcip1.ee.uni-sb.de .)  North American users should
  585.     access rtfm.mit.edu, directory /pub/usenet/comp.os.msdos.programmer,
  586.     file names T_S_P*_3.
  587.  
  588. section 6. Other hardware questions and problems
  589. ================================================
  590.  
  591. Subject:  601. Which 80x86 CPU is running my program?
  592.  
  593.     (rev: 16 Aug 1993)  According to an article posted by Michael
  594.     Davidson, Intel's approved code for distinguishing among 8086,
  595.     80286, 80386, and 80486 and for detecting the presence of an 80287
  596.     or 80387 is published in Intel's 486SX processor manual (order
  597.     number 240950-001).  David Kirschbaum's improved version of this is
  598.     downloadable as
  599.         pd1:<msdos.sysutl>cpuid593.zip from Simtel
  600.         /pc/sysinfo/cpuid593.zip from Garbo.
  601.  
  602.     According to an article posted by its author, WCPU knows the
  603.     differences between DX and SX varieties of 386 and 486 chips, and
  604.     can detect a math coprocessor and a Pentium.  It's downloadable as
  605.         pd1:<msdos.sysinfo>wcpu050.zip at Simtel
  606.         /pc/sysinfo/wcpu050.zip at Garbo.
  607.  
  608. Subject:  602. How can a C program send control codes to my printer?
  609.  
  610.     If you just fprintf(stdprn, ...), C will translate some of your
  611.     control codes.  The way around this is to reopen the printer in
  612.     binary mode:
  613.  
  614.         prn = fopen("PRN", "wb");
  615.  
  616.     You must use a different file handle because stdprn isn't an lvalue.
  617.     By the way, PRN or LPT1 must not be followed by a colon in DOS 5.0.
  618.  
  619.     There's one special case, Ctrl-Z (ASCII 26), the DOS end-of-file
  620.     character.  If you try to send an ASCII 26 to your printer, DOS
  621.     simply ignores it.  To get around this, you need to reset the
  622.     printer from "cooked" to "raw" mode.  Microsoft C users must use int
  623.     21 function 44, "get/set device information".  Turbo C and Borland
  624.     C++ users can use ioctl to accomplish the same thing:
  625.  
  626.         ioctl(fileno(prn), 1, ioctl(fileno(prn),0) & 0xFF | 0x20, 0);
  627.  
  628.     An alternative approach is simply to write the printer output into a
  629.     disk file, then copy the file to the printer with the /B switch.
  630.  
  631.     A third approach is to bypass DOS functions entirely and use the
  632.     BIOS printer functions at INT 17.  If you also fprintf(stdprn,...)
  633.     in the same program, you'll need to use fflush( ) to synchronize
  634.     fprintf( )'s buffered output with the BIOS's unbuffered.
  635.  
  636.     By the way, if you've opened the printer in binary mode from a C
  637.     program, remember that outgoing \n won't be translated to carriage
  638.     return/line feed.  Depending on your printer, you may need to send
  639.     explicit \n\r sequences.
  640.  
  641. Subject:  603. How can I redirect printer output to a file?
  642.  
  643.     Please see section 4, "Disks and files", for the answer.
  644.  
  645. Subject:  604. Which video adapter is installed?
  646.  
  647.     The technique below should work if your BIOS is not too old.  It
  648.     uses three functions from INT 10, the BIOS video interrupt.  (If
  649.     you're using a Borland language, you may not have to do this the
  650.     hard way.  Look for a function called DetectGraph or something
  651.     similar.)
  652.  
  653.     Set AH=12h, AL=0, BL=32h; INT 10h.  If AL is 12h, you have a VGA.
  654.     If not, set AH=12h, BL=10h; INT 10h.  If BL is 0,1,2,3, you have an
  655.     EGA with 64,128,192,256K memory.  If not, set AH=0Fh; INT 10h.  If
  656.     AL is 7, you have an MDA (original monochrome adapter) or Hercules;
  657.     if not, you have a CGA.
  658.  
  659.     This worked when tested with a VGA, but SB had no other adapter
  660.     types to test it with.
  661.  
  662. Subject:  605. How do I switch to 43- or 50-line mode?
  663.  
  664.     pd1:<msdos.screen>vidmode.zip, downloadable from Simtel, contains
  665.     .COM utilities and .ASM source code.
  666.  
  667. Subject:  606. How can I find the Microsoft mouse position and button
  668.                status?
  669.  
  670.     Use INT 33 function 3, described in Ralf Brown's interrupt list.
  671.  
  672.     The Windows manual says that the Logitech mouse is compatible with
  673.     the Microsoft one, so the interrupt will probably work the same.
  674.  
  675.     Also, many files are downloadable from pd1:<msdos.mouse> at Simtel.
  676.  
  677. Subject:  607. How can I access a specific address in the PC's memory?
  678.  
  679.     First check the library that came with your compiler.  Many vendors
  680.     have some variant of peek and poke functions; in Turbo Pascal use
  681.     the pseudo-arrays Mem, MemW, and MemL.  As an alternative, you can
  682.     construct a far pointer:  use Ptr in Turbo Pascal, MK_FP in the
  683.     Turbo C family, and FP_OFF and FP_SEG in Microsoft C.
  684.  
  685.     Caution:  Turbo C and Turbo C++ also have FP_OFF and FP_SEG macros,
  686.     but they can't be used to construct a pointer.  In Borland C++ those
  687.     macros work the same as in Microsoft C, but MK_FP is easier to use.
  688.  
  689.     By the way, it's not useful to talk about "portable" ways to do
  690.     this.  Any operation that is tied to a specific memory address is
  691.     not likely to work on another kind of machine.
  692.  
  693. Subject:  608. How can I read or write my PC's CMOS memory?
  694.  
  695.     (rev: 24 Sep 1993) There are a great many public-domain utilities
  696.     that do this.  These are downloadable from Simtel:
  697.  
  698.     pd1:<msdos.at>
  699.     cmos14.zip     5965  920817  Saves/restores CMOS to/from file
  700.     cmoser11.zip  28323  910721  386/286 enhanced CMOS setup program
  701.     cmosram.zip   76096  920214  Save AT/386/486 CMOS data to file and restore
  702.     rom2.zip      15692  900131  Save AT and 386 CMOS data to file and restore
  703.     setup21.zip   18172  880613  Setup program which modifies CMOS RAM
  704.     viewcmos.zip  11068  900225  Display contents of AT CMOS RAM, w/C source
  705.  
  706.     A program to check and display CMOS memory (but not write to it) is
  707.     downloadable as part of
  708.         /pc/ts/tsutle22.zip at Garbo
  709.         pd1:<msdos.sysutl>tsutle22.zip at Simtel.
  710.  
  711.     Good reports of CMOS299.ZIP, available in the pc.dir directory of
  712.     cantva.canterbury.ac.nz [132.181.30.3], have been posted.
  713.  
  714.     Of the above, SB's only experience is with CMOSRAM, which seems to
  715.     work fine.  It contains an excellent (and witty) .DOC file that
  716.     explains the hardware involved and gives specific recommendations
  717.     for preventing disaster or recovering from it.  It's $5 shareware.
  718.  
  719.     Robert Jourdain's {Programmer's Problem Solver for the IBM PC, XT,
  720.     and AT} has code for accessing the CMOS RAM, according to an article
  721.     posted in this newsgroup.
  722.  
  723. Subject:  609. How can I access memory beyond 640K?
  724.  
  725.     (rev: 14 Sep 1993) This is a legitimate FAQ, in that it is frequently
  726.     asked.  But there is no single agreed-upon answer.  Please see the
  727.     separate article called "How to access memory above 640K" in
  728.     comp.os.msdos.programmer and in faqp*.zip at Simtel and Garbo.
  729.  
  730.     The 29 June 1993 issue (xii:12) of {PC Magazine} carries an article,
  731.     "How DOS Programs Can Use Over 1MB of RAM" on pages 302-304.
  732.  
  733. Subject:  610. Where can I find a list of 80x86 opcodes?
  734.  
  735.     (new: 2 May 1993)  It's part of a rather long file, the 8 Dec 1992
  736.     edition of the Info-IBMPC Digest (V92 #185), downloadable as
  737.     pd2:<archives.ibmpc>9212.1-txt at Simtel.  (Note: pd2, not
  738.     pd1.)  Opcodes for the 8086 through 80386 are listed.
  739.  
  740. section 7. Other software questions and problems
  741. ================================================
  742.  
  743. Subject:  701. How can a program reboot my PC?
  744.  
  745.     (rev: 11 Sep 1993) You can generate a "cold" boot or a "warm" boot.
  746.     A cold boot is the same as turning the power off and on; a warm boot
  747.     is the same as Ctrl-Alt-Del and skips the power-on self test.
  748.  
  749.     For a warm boot, store the hex value 1234 in the word at 0040:0072.
  750.     For a cold boot, store 0 in that word.  Then, if you want to live
  751.     dangerously, jump to address FFFF:0000.  Here's C code to do it:
  752.  
  753.         /* WARNING:  data loss possible */
  754.         void bootme(int want_warm)  /* arg 0 = cold boot, 1 = warm */ {
  755.             void (far* boot)(void) = (void (far*)(void))0xFFFF0000UL;
  756.             unsigned far* type = (unsigned far*)0x00400072UL;
  757.             *type = (want_warm ? 0x1234 : 0);
  758.             (*boot)( );
  759.         }
  760.  
  761.     What's wrong with that method?  It will boot right away, without
  762.     closing files, flushing disk caches, etc.  If you boot without
  763.     flushing a write-behind disk cache (if one is running), you could
  764.     lose data or even trash your hard drive.
  765.  
  766.     There are two methods of signaling the cache to flush its buffers:
  767.     (1) simulate a keyboard Ctrl-Alt-Del in the keystroke translation
  768.     function of the BIOS (INT 15 function 4F; but see notes below), and
  769.     (2) issue a disk reset (DOS function 0D).  Most disk-cache programs
  770.     hook one or both of those interrupts, so if you use both methods
  771.     you'll probably be safe.
  772.  
  773.     When user code simulates a Ctrl-Alt-Del, one or more of the programs
  774.     that have hooked INT 15 function 4F can ask that the key be ignored by
  775.     clearing the carry flag.  For example, HyperDisk does this when it
  776.     has started but not finished a cache flush.  So if the carry flag
  777.     comes back cleared, the boot code has to wait a couple of clock
  778.     ticks and then try again.  (None of this matters on older machines
  779.     whose BIOS can't support 101- or 102-key keyboards; see "What is the
  780.     SysRq key for?" in section 3, "Keyboard".)
  781.  
  782.     C code that tries to signal the disk cache (if any) to flush is
  783.     given below.  Turbo Pascal code by Timo Salmi that does more or less
  784.     the same job may be found at question 49 (as of this writing) in the
  785.     Turbo Pascal FAQ in comp.lang.pascal, and is downloadable in
  786.     FAQPAS2.TXT in
  787.         /pc/ts/tsfaqp15.zip at Garbo
  788.         pd1:<msdos.info>tsfaqp15.zip at Simtel.
  789.  
  790.     Here's C code that reboots after trying to signal the disk cache:
  791.         #include <dos.h>
  792.         void bootme(int want_warm)  /* arg 0 = cold boot, 1 = warm */ {
  793.             union REGS reg;
  794.             void    (far* boot)(void) = (void (far*)(void))0xFFFF0000UL;
  795.             unsigned far* boottype    =     (unsigned far*)0x00400072UL;
  796.             char     far* shiftstate  =         (char far*)0x00400017UL;
  797.             unsigned      ticks;
  798.             int           time_to_waste;
  799.             /* Simulate reception of Ctrl-Alt-Del: */
  800.             for (;;) {
  801.                 *shiftstate |= 0x0C;    /* turn on Ctrl & Alt */
  802.                 reg.h.ah = 0x4F;        /* see notes below */
  803.                 reg.h.al = 0x53;        /* 0x53 = Del's scan code */
  804.                 reg.x.cflag = 1;        /* sentinel for ignoring key */
  805.                 int86(0x15, ®, ®);
  806.                 /* If carry flag is still set, we've finished. */
  807.                 if (reg.x.cflag)
  808.                     break;
  809.                 /* Else waste some time before trying again: */
  810.                 reg.h.ah = 0;
  811.                 int86(0x1A, ®, ®);/* system time into CX:DX */
  812.                 ticks = reg.x.dx;
  813.                 for (time_to_waste = 3;  time_to_waste > 0;  ) {
  814.                     reg.h.ah = 0;
  815.                     int86(0x1A, ®, ®);
  816.                     if (ticks != reg.x.dx)
  817.                         ticks = reg.x.dx , --time_to_waste;
  818.                 }
  819.             }
  820.             /* Issue a DOS disk reset request: */
  821.             reg.h.ah = 0x0D;
  822.             int86(0x21, ®, ®);
  823.             /* Set boot type and boot: */
  824.             *boottype = (want_warm ? 0x1234 : 0);
  825.             (*boot)( );
  826.         }
  827.  
  828.     Reader Timo Salmi reported (26 July 1993) that the INT 15 AH=4F call
  829.     may not work on older PCs (below AT, XT2, XT286), according to Ralf
  830.     Brown's interrupt list.
  831.  
  832.     Reader Roger Fulton reported (1 July 1993) that INT 15 AH=4F call
  833.     above hangs even a modern PC "ONLY when ANSI.SYS [is] loaded high
  834.     using EMM386.EXE.  (Other things loaded high with EMM386.EXE were
  835.     OK; ANSI.SYS loaded high with QEMM386.SYS was OK; ANSI.SYS loaded
  836.     low with EMM386.EXE installed was OK.)"  His solution was to use
  837.     only the disk reset, INT 21 function 0D, which does flush SMARTDRV,
  838.     then wait five seconds in hopes that any other disk-caching
  839.     software would have time to flush its queue.
  840.  
  841.     If you have a more bulletproof solution, please send it to the
  842.     editor.
  843.  
  844.     Reader Per Bergland reported (10 Sep 1993) that the jump to
  845.     FFFF:0000 will not work in Windows or other protected-mode programs.
  846.     (For example, when the above reboot code ran in a DOS session under
  847.     Windows, a box with "waiting for system shutdown" appeared.  The PC
  848.     hung and had to be reset by cycling power.)  His solution, which does
  849.     a cold boot not a warm boot, is to pulse pin 0 of the 8042 keyboard
  850.     controller, which is connected to the CPU's "reset" line.  He has
  851.     tested the following code on various Compaqs, and expects it will
  852.     work for any AT-class machine; he cautions that you must first flush
  853.     the disk cache as indicated above.
  854.  
  855.             cli
  856.         @@WaitOutReady:   { Busy-wait until 8042 is ready for new command}
  857.             in al,64h         { read 8042 status byte}
  858.             test al,00000010b { Bit 1 of status indicates input buffer full }
  859.             jnz @@WaitOutReady
  860.             mov al,0FEh       { Pulse "reset" = 8042 pin 0 }
  861.             out 64h,al
  862.             { The PC will reboot now }
  863.  
  864. Subject:  702. How can I time events with finer resolution than the
  865.                system clock's 55 ms (about 18 ticks a second)?
  866.  
  867.     (rev: 28 Aug 1993) The following files, among others, are
  868.     downloadable from Simtel:
  869.  
  870.     pd1:<msdos.at>
  871.     atim.zip       4783  881126  Precision program timing for AT
  872.  
  873.     pd1:<msdos.c>
  874.     millisec.zip  37734  911205  MSC/asm src for millisecond res timing
  875.     mschrt3.zip   53708  910605  High-res timer toolbox for MSC 5.1
  876.     msec_12.zip    8484  920320  High-def millisec timer v1.2 (C,ASM)
  877.     ztimer11.zip  77625  920428  Microsecond timer for C, C++, ASM
  878.         (also at Garbo as /pc/c/ztimer11.zip)
  879.  
  880.     pd1:<msdos.turbo-c>
  881.     tchrt3.zip    53436  910606  High-res timer toolbox for Turbo C 2.0
  882.     tctimer.arc   20087  891030  High-res timing of events for Turbo C
  883.         (same as /pc/c/tctimer.zoo at Garbo; both are version 1.0)
  884.  
  885.     For Turbo Pascal users, source and object code are downloadable in
  886.         pd1:<msdos.turbopas>bonus507.zip at Simtel
  887.         /pc/turbopas/bonus507.zip at Garbo.
  888.     Also see "Q: How is millisecond timing done?" in FAQPAS.TXT,
  889.     downloadable as
  890.         /pc/ts/tsfaqp15.zip at Garbo
  891.         pd1:<msdos.info>tsfaqp15.zip at Simtel.
  892.  
  893. Subject:  703. How can I find the error level of the previous program?
  894.  
  895.     (rev: 16 Aug 1993)  First, which previous program are you talking
  896.     about?  If your current program ran another one, when the child
  897.     program ends its error level is available to the program that
  898.     spawned it.  Most high-level languages provide a way to do this; for
  899.     instance, in Turbo Pascal it's Lo(DosExitCode) and the high byte
  900.     gives the way in which the child terminated.  In Microsoft C, the
  901.     exit code of a synchronous child process is the return value of the
  902.     spawn-type function that creates the process.
  903.  
  904.     If your language doesn't have a function to return the error code
  905.     of a child process, you can use INT 21 function 4D (get return
  906.     code).  By the way, this will tell you the child's exit code and the
  907.     manner of its ending (normal, Ctrl-C, critical error, or TSR).
  908.  
  909.     It's much trickier if the current program wants to get the error
  910.     level of the program that ran and finished before this one started.
  911.     G.A.Theall has published source and compiled code to do this; the
  912.     code is downloadable as
  913.         pd1:<msdos.batutl>errlvl13.zip at Simtel
  914.         /pc/batchutil/errlvl12.zip (an older version) at Garbo.
  915.     (The code uses undocumented features in DOS 3.3 through 5.0.  Theall
  916.     says in the .DOC file that the values returned under 4DOS or other
  917.     replacements won't be right.)
  918.  
  919. Subject:  704. How can a program set DOS environment variables?
  920.  
  921.     (rev: 13 June 1993)  Program functions that read or write "the
  922.     environment" typically access only the program's copy of it.  What
  923.     this Q really wants to do is to modify the active environment, the
  924.     one that is affected by SET commands in batch files or at the DOS
  925.     prompt.  You need to do some programming to find the active
  926.     environment, and that depends on the version of DOS.
  927.  
  928.     A fairly well-written article in {PC Magazine} 28 Nov 1989
  929.     (viii:20), pages 309-314, explains how to find the active
  930.     environment, and includes Pascal source code.  The article hints at
  931.     how to change the environment, and suggests creating paths longer
  932.     than 128 characters as one application.
  933.  
  934.     Now as for downloadable source code, there are many possibilities.
  935.     SB looked at some of these, and liked
  936.         pd1:<msdos.envutil>rbsetnv1.zip at Simtel
  937.         /pc/envutil/rbsetnv1.zip at Garbo
  938.     the best.  It includes some utilities to manipulate the environment,
  939.     with source code in C.  A newer program is
  940.         pd1:<msdos.batutl>strings2.zip at Simtel
  941.         part of /pc/pcmag/vol11n22.zip at Garbo,
  942.     which is the code from {PC Magazine} 22 Dec 1992 (xi:22).
  943.  
  944.     You can also use a call to INT 2E, Pass Command to Interpreter for
  945.     Execution; see Ralf Brown's interrupt list for details and cautions.
  946.  
  947. Subject:  705. How can I change the switch character to - from /?
  948.  
  949.     Under DOS 5.0, you can't -- not completely, anyway.  INT 21 function
  950.     3700, get switch character, always returns a '/' (hex 2F) -- and the
  951.     DOS commands don't even call that function, but hard code '/' as the
  952.     switch character.
  953.  
  954.     Some history:  DOS used to let you change the switch character by
  955.     using SWITCHAR= in CONFIG.SYS or by calling DOS function 3701.  DOS
  956.     commands and other programs called DOS function 3700 to find out the
  957.     switch character.  If you changed the switch character to '-' (the
  958.     usual choice), you could then type "dir c:/c700 -p" rather than "dir
  959.     c:\c700 /p".  Under DOS 4.0, the DOS commands ignored the switch
  960.     character but functions 3700 and 3701 still worked and could be used
  961.     by other programs.  Under DOS 5.0, even those functions no longer
  962.     work, though all DOS functions still accept '/' or '\' in file
  963.     specs.
  964.  
  965.     You can reactivate the functions to get and set switchar by using
  966.     programs like SLASH.ZIP or the sample TSR called SWITCHAR in
  967.     amisl091.zip (see "How can I write a TSR?", below.)  DOS commands
  968.     will still use the slash, but non-DOS programs that call DOS func-
  969.     tion 3700 will use your desired switch character.  (DOS replacements
  970.     like 4DOS may honor the switch character for internal commands.)
  971.  
  972.     Some readers may wonder why this is even an issue.  Making '-' the
  973.     switch character frees up the front slash to separate names in the
  974.     path part of a file spec.  This is easier for the ten-fingered to
  975.     type, and it's one less difference to remember for commuters between
  976.     DOS and Unix.  The switch character is the only issue, since all the
  977.     INT 21 functions accept '/' or '\' to separate directory names.
  978.  
  979. Subject:  706. Why does my interrupt function behave strangely?
  980.  
  981.     (rev: 24 Sep 1993)  Interrupt service routines can be tricky,
  982.     because you have to do some things differently from "normal"
  983.     programs.  If you make a mistake, debugging is a pain because the
  984.     symptoms may not point at what's wrong.  Your machine may lock up or
  985.     behave erratically, or just about anything else can happen.  Here
  986.     are some things to look for.  (See the next Q for general help
  987.     before you have a problem.)
  988.  
  989.     First, did you fail to set up the registers at the start of your
  990.     routine?  When your routine begins executing, you can count on
  991.     having CS point to your code segment and SS:SP point to some valid
  992.     stack (of unknown length), and that's it.  In particular, an
  993.     interrupt service routine must set DS to DGROUP before accessing any
  994.     data in its data segments.  (If you're writing in a high-level
  995.     language, the compiler may generate this code for you automatically;
  996.     check your compiler manual.  For instance, in Borland and Microsoft
  997.     C, give your function the "interrupt" attribute.)
  998.  
  999.     Did you remember to turn off stack checking when compiling your
  1000.     interrupt server and any functions it calls?  The stack during the
  1001.     interrupt is not where the stack-checking code expects it to be.
  1002.     (Caution:  Some third-party libraries have stack checking compiled
  1003.     in, so you can't call them from your interrupt service routine.)
  1004.  
  1005.     Next, are you calling any DOS functions (INT 21, 25, or 26) in your
  1006.     routine?  DOS is not re-entrant.  This means that if your interrupt
  1007.     happens to be triggered while the CPU is executing a DOS function,
  1008.     calling another DOS function will wreak havoc.  (Some DOS functions
  1009.     are fully re-entrant, as noted in Ralf Brown's interrupt list.
  1010.     Also, your program can test, in a way too complicated to present
  1011.     here, when it's safe to call non-re-entrant DOS functions.  See INT
  1012.     28 and functions 34, 5D06, 5D0B of INT 21; and consult {Undocumented
  1013.     DOS} by Andrew Schulman.  Your program must read both the "InDOS
  1014.     flag" and the "critical error flag".)
  1015.  
  1016.     Is a function in your language library causing trouble?  Does it
  1017.     depend on some initializations done at program startup that is no
  1018.     longer available when the interrupt executes?  Does it call DOS (see
  1019.     preceding paragraph)?  For example, in both Borland and Microsoft C
  1020.     the memory-allocation functions (malloc, etc..) and standard I/O
  1021.     functions (scanf, printf) call DOS functions and also depend on
  1022.     setups that they can't get at from inside an interrupt.  Many other
  1023.     library functions have the same problem, so you can't use them
  1024.     inside an interrupt function without special precautions.
  1025.  
  1026.     Is your routine simply taking too long?  This can be a problem if
  1027.     you're hooking on to the timer interrupt, INT 1C or INT 8.  Since
  1028.     that interrupt expects to be called 18.2 times a second, your
  1029.     routine -- plus any others hooked to the same interrupts -- must
  1030.     execute in less than 55 ms.  If they use even a substantial fraction
  1031.     of that time, you'll see significant slowdowns of your foreground
  1032.     program.  A good discussion is downloadable as
  1033.         pub/msdos/SIMTEL20-mirror/info/intshare.zip at ni.funet.fi
  1034.         pd1:<msdos.info>intshare.zip at Simtel.
  1035.  
  1036.     Did you forget to restore all registers at the end of your routine?
  1037.  
  1038.     Did you chain improperly to the original interrupt?  You need to
  1039.     restore the stack to the way it was upon entry to your routine, then
  1040.     do a far jump (not call) to the original interrupt service routine.
  1041.     (The process is a little different in high-level languages.)
  1042.  
  1043. Subject:  707. How can I write a TSR (terminate-stay-resident utility)?
  1044.  
  1045.     (rev: 20 June 1993)  There are books, and there's code to download.
  1046.  
  1047.     First, the books:
  1048.  
  1049.     - Ray Duncan's {Advanced MS-DOS}, ISBN 1-55615-157-8, gives a brief
  1050.       checklist intended for experienced programmers.  The ISBN is for
  1051.       the second edition, through DOS 4; but check to see whether the
  1052.       DOS 5 version is available yet.
  1053.  
  1054.     - {DOS 5:  A Developer's Guide} by Al Williams, ISBN 1-55851-177-6,
  1055.       goes into a little more detail, 90 pages worth!
  1056.  
  1057.     - Pascal programmers might look at {The Ultimate DOS Programmer's
  1058.       Manual} by John Mueller and Wallace Wang, ISBN 0-8306-3534-3, for
  1059.       an extended example in mixed Pascal and assembler.
  1060.  
  1061.     - For a pure assembler treatment, check Steven Holzner's {Advanced
  1062.       Assembly Language}, ISBN 0-13-663014-6.  He has a book with the
  1063.       same title out from Brady Press, but it's about half as long as
  1064.       this one.
  1065.  
  1066.     Next, the code.  Some of it is companion code to published articles,
  1067.     which are also listed below:
  1068.  
  1069.     - The Alternate Multiplex Interrupt Specification, downloadable as
  1070.         pd1:<msdos.info>altmpx35.zip at Simtel
  1071.         /pc/programming/altmpx35.zip at Garbo
  1072.         /afs/cs/user/ralf/pub/altmpx35.zip at cs.cmu.edu
  1073.  
  1074.     - Ralf Brown's assembly-language implementation of the spec, with
  1075.       utilities in C, downloadable as
  1076.         pd1:<msdos.asmutl>amisl091.zip at Simtel
  1077.         /pc/c/amisl091.zip at Garbo
  1078.         /afs/cs/user/ralf/pub/amisl091.zip at cs.cmu.edu
  1079.  
  1080.     - Douglas Boling's MASM template for a TSR is downloadable as
  1081.         pd1:<msdos.asmutl>template.zip at Simtel.
  1082.  
  1083.     - A posted article mentions Boling's "Strategies and Techniques for
  1084.       Writing State-of-the-Art TSRs that Exploit MS-DOS 5", Microsoft
  1085.       Systems Journal, Jan-Feb 1992, Volume 7, Number 1, pages 41-59,
  1086.       with examples downloadable in
  1087.           pd1:<msdos.msjournal>msjv7-1.zip at Simtel
  1088.  
  1089.     - code for Al Stevens's "Writing Terminate-and-Stay-Resident
  1090.       Programs", Computer Language, February 1988, pages 37-48 and March
  1091.       1988, pages 67-76 is downloadable as
  1092.         pd1:<msdos.c>tsrc.zip at Simtel
  1093.  
  1094.     - software examples to accompany Kaare Christian's "Using Microsoft
  1095.       C Version 5.1 to Write Terminate-and-Stay-Resident Programs",
  1096.       Microsoft Systems Journal, September 1988, Volume 3, Number 5,
  1097.       pages 47-57 are downloadable as
  1098.           pd1:<msdos.msjournal>msjv3-5.arc at Simtel
  1099.  
  1100.     Finally, there are commercial products, of which TesSeRact (for
  1101.     C-language TSRs) is one of the best known.
  1102.  
  1103. Subject:  708. How can I write a device driver?
  1104.  
  1105.     Many books answer this in detail.  Among them are {Advanced MS-DOS}
  1106.     and {DOS 5: A Developer's Guide}, cited in the preceding Q.
  1107.     Michael Tischer's {PC System Programming}, ISBN 1-55755-036-0, has
  1108.     an extensive treatment, as does Dettman and Kyle's {DOS Programmer's
  1109.     Reference: 2d Edition}, ISBN 0-88022-458-4.  For a really in-depth
  1110.     treatment, look for a specialized book like Robert Lai's {Writing
  1111.     MS-DOS Device Drivers}, ISBN 0-201-13185-4.
  1112.  
  1113. Subject:  709. What can I use to manage versions of software?
  1114.  
  1115.     (rev: 21 Aug 1993) A port of the Unix RCS utility is downloadable as
  1116.         pd1:<msdos.gnuish>rcs55ax.zip (EXE and docs) from Simtel
  1117.         pd1:<msdos.gnuish>rcs55as.zip (source) from Simtel
  1118.         /pc/unix/alrcs5ex.zip (EXE and docs ?) from Garbo.
  1119.     This is no longer limited to one-character extensions on filenames
  1120.     (.CPP and .BAS are now OK).
  1121.  
  1122.     An RCS56 is available at a number of archive sites, but it appears
  1123.     to be unauthorized.  In response to a query, Keith Petersen, Simtel
  1124.     administrator, said that RCS56 was removed from Simtel at the
  1125.     author's request because it did not contain source code and thus was
  1126.     in violation of the GNU copyleft.
  1127.  
  1128.     As for commercial software, SB posted a question asking for readers'
  1129.     experiences in July 1993 and seven readers responded.  PVCS from
  1130.     Intersolv (formerly Polymake) got five positive reviews, though
  1131.     several readers commented that it's expensive; RCS from MKS got one
  1132.     positive and one negative review; Burton TLIB got one negative
  1133.     review; DRTS from ILSI got one positive review.
  1134.  
  1135. Subject:  710. What's this "null pointer assignment" after my C program
  1136.                executes?
  1137.  
  1138.     (rev: 17 Sep 1993)  Somewhere in your program, you assigned a value
  1139.     _through_ a pointer without first assigning a value _to_ the
  1140.     pointer.  (This might have been something like a strcpy or memcpy
  1141.     with a pointer as its first argument, not necessarily an actual
  1142.     assignment statement.) Your program may look like it ran correctly,
  1143.     but if you get this message you can be certain that there's a bug
  1144.     somewhere.
  1145.  
  1146.     Microsoft and Borland C, as part of their exit code (after a return
  1147.     from your main function), check whether the location 0000 in your
  1148.     data segment contains a different value from what you started with;
  1149.     if so, they infer that you must have used an uninitialized pointer.
  1150.     This implies that the message will appear at the end of execution of
  1151.     your program regardless of where the error actually occurred.
  1152.  
  1153.     To track down the problem, you can put exit( ) statements at various
  1154.     spots in the program and narrow down where the uninitialized pointer
  1155.     is being used by seeing which added exit( ) makes the null-pointer
  1156.     message disappear.  Or, in the debugger, set a watch at location
  1157.     0000 in your data segment, assuming you're in small or medium model.
  1158.     (If data pointers are 32 bits, as in the compact and large models, a
  1159.     null pointer will overwrite the interrupt vectors at 0000:0000 and
  1160.     probably lock up your machine.)
  1161.  
  1162.     Under MSC/C++ 7.0, you can declare the undocumented library function
  1163.  
  1164.         extern _cdecl _nullcheck(void);
  1165.  
  1166.     and then sprinkle calls to _nullcheck( ) through your program at
  1167.     regular intervals.
  1168.  
  1169.     Borland's TechFax document #TI726 discusses the null pointer
  1170.     assignment from a Borland point of view.  It's one of many documents
  1171.     downloadable as part of
  1172.         pd1:<msdos.turbo-c>bchelp10.zip at Simtel
  1173.         /pc/turbopas/bchelp10.zip at Garbo.
  1174.  
  1175. Subject:  711. How can my program tell if it's running under Windows?
  1176.  
  1177.     (rev: 18 Apr 1993)  Set AX=4680 and execute INT 2F.  If AX contains
  1178.     0, you're in Windows real mode or standard mode (or under the DOS
  1179.     5.0 shell).  Otherwise, set AX=1600 and INT 2F.  If AL does not
  1180.     contain 0 or 80, you're in Windows 386 enhanced mode.  See {PC
  1181.     Magazine} 24 Nov 1992 (xi:20), pages 492-493.
  1182.  
  1183.     When Windows 3.0 or 3.1 is running, the DOS environment will contain
  1184.     a definition of the string windir, in lower case.
  1185.  
  1186.     For more information, see {PC Magazine} 26 May 1992 (xi:10) pages
  1187.     345-346.  A program, WINMODE, is available as part of
  1188.         pd1:<msdos.pcmag>vol11n10.zip at Simtel
  1189.         /pc/pcmag/vol11n10.zip at Garbo.
  1190.  
  1191. Subject:  712. How do I copyright software that I write?
  1192.  
  1193.     (rev: 9 Sep 1993) The following is adapted (and greatly condensed)
  1194.     from chapter 4 of the Chicago Manual of Style (13th edition, ISBN
  1195.     0-226-10390-0).  Disclaimer:  This is not written by a lawyer, and
  1196.     is not legal advice.  Also, there are very likely to be differences
  1197.     in copyright law among nations.  No matter where you live, if
  1198.     significant money may be involved, get legal advice.
  1199.  
  1200.     That said, in the U.S. (at least), when you write something, you own
  1201.     the copyright.  (The most significant exception to programmers is
  1202.     "works made for hire", i.e., something you write because your
  1203.     employer or client pays you to.  A contract, agreed in advance, can
  1204.     vest the copyright in the programmer even if an employee.)  You
  1205.     don't have to register the work with the Copyright Office unless
  1206.     (until) the copyright is infringed and you intend to bring suit;
  1207.     however, it is easier to recover damages in court if you did
  1208.     register the work within three months of publication.
  1209.  
  1210.     From paragraph 4.16 of the Chicago Manual:  "... the [copyright]
  1211.     notice consists of three parts: (1) the symbol [C-in-a-circle]
  1212.     (preferred because it also suits the requirements of the Universal
  1213.     Copyright Convention), the word 'Copyright', or the abbreviation
  1214.     'Copr.', (2) a date--the year of first publication, and (3) the name
  1215.     of the copyright owner.  Most publishers also add the phrase 'All
  1216.     rights reserved' because it affords some protection in Central and
  1217.     South American countries ...."  Surprise:  "(C)" is legally not the
  1218.     same as the C-in-a-circle, so those of us who are ASCII-bound must
  1219.     use the word or the abbreviation.
  1220.  
  1221.     You can download a much more comprehensive treatment from the
  1222.     Internet.  Terry Carroll posts a six-part Copyright FAQ to
  1223.     misc.legal, news.answers and other groups.
  1224.  
  1225.  
  1226. (continued in part 4)
  1227.